package com.wang.jmonkey.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.wang.jmonkey.common.utils.DateUtil;
import com.wang.jmonkey.common.utils.UserUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

/**
 * @Description MyBatis 配置
 * @Author HeJiawang
 * @Date 2021/5/21 17:18
 */
@Configuration
public class MyBatisAutoConfig {

    /**
     * 自动填充字段
     */
    @Configuration
    class DataMetaObjectHandler implements MetaObjectHandler {

        private final static String CREATED_BY = "createdBy";
        private final static String CREATED_TIME = "createdTime";

        private final static String UPDATED_BY = "updatedBy";
        private final static String UPDATED_TIME = "updatedTime";

        @Override
        public void insertFill(MetaObject metaObject) {
            setFieldValByName(CREATED_BY, UserUtil.getUser().getId(), metaObject);
            setFieldValByName(CREATED_TIME, DateUtil.current(), metaObject);
        }

        @Override
        public void updateFill(MetaObject metaObject) {
            setFieldValByName(UPDATED_BY, UserUtil.getUser().getId(), metaObject);
            setFieldValByName(UPDATED_TIME, DateUtil.current(), metaObject);
        }
    }
}
